package cn.com.jslh.boarpro.main;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.main.view.fragment.MenuFragment;
import cn.com.jslh.boarpro.util.ModuleUtil;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;
import cn.com.jslh.boarpro.util.template.AddTempBaseItem;
import cn.com.jslh.boarpro.util.template.BaseAction;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

import static cn.com.jslh.boarpro.base.BaseApplication.MODULETYPE;

public class MainDetilActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.viewPage)
    ViewPager viewPage;
    @BindView(R.id.viewpagertab)
    SmartTabLayout viewpagertab;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    @BindView(R.id.tl_custom)
    Toolbar toolbar;

    public static ModuleUtil moduleUtil;
    private FragmentPagerItemAdapter adapter;
    private SmartTabLayout viewPagerTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_detail);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
        toolbar.inflateMenu(R.menu.menu_detil);
        toolbar.setOnMenuItemClickListener(this);
        initPages(moduleUtil);
        SPUtils.getInstance().put(MODULETYPE,moduleUtil.getSearchType());
        MenuFragment.setModuleUtil(moduleUtil);
        DataBaseUtil.clearTable(this, BaseAction.class);
        DataBaseUtil.clearTable(this, AddTempBaseItem.class);
    }


    private void initPages(ModuleUtil moduleUtil) {
        viewPage.setAdapter(initAdapter(moduleUtil));
        viewPagerTab.setViewPager(viewPage);
        title.setText(moduleUtil.getMenuName());
    }

    private FragmentPagerItemAdapter initAdapter(ModuleUtil moduleUtil) {
        FragmentPagerItems.Creator items = FragmentPagerItems.with(this);
        if (moduleUtil.getPages() != null && moduleUtil.getPages().size() > 0) {
            for (int i = 0; i < moduleUtil.getPages().size(); i++) {
                ModuleUtil.Page page = moduleUtil.getPages().get(i);
                items.add(page.getModuleName(), page.getFragmentClass());
            }
        }
        return new FragmentPagerItemAdapter(getSupportFragmentManager(), items.create());
    }

    public ViewPager getViewPage() {
        return viewPage;
    }

    public void setViewPage(ViewPager viewPage) {
        this.viewPage = viewPage;
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEventMainThread(ModuleUtil moduleUtil) {
    }

    public static void setMainModule(ModuleUtil module) {
        EventBus.getDefault().post(module);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @OnClick(R.id.title)
    public void onViewClicked() {
        if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
            drawerLayout.closeDrawer(Gravity.LEFT);
        } else {
            drawerLayout.openDrawer(Gravity.LEFT);
//            MenuFragment.setModuleUtil(moduleUtil);
        }
    }


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.up_action:
                EventBus.getDefault().post(new Action(R.id.up_action));
                break;
        }
        return true;
    }

    public class Action {
        private int id;

        public Action(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }
}
